{% extends "base.html" %}
{% import editor_macro(request) as editor %}

{% block head %}
    {{ editor.head() }}
{% endblock head %}

{% block title %}{{ qa_config.qa_title }}{% endblock title %}
{% block subtitle %}{{ qa_config.qa_title }}{% endblock subtitle %}

{% block content %}
    {% if qa_config.qa_content_head %}
        {{ qa_config.qa_content_head|safe }}
    {% endif %}

    <section id="bo_w" class="fs-4">
        <h2 class="blind">{{ qa_config.qa_title }} 写作</h2>
        <!-- 写一篇文章/开始修正 { -->
        <form name="fwrite" id="fwrite" action="{{ url_for('qa_write_update') }}?{{ request.query_params }}" onsubmit="return fwrite_submit(this);" method="post" enctype="multipart/form-data" autocomplete="off">
            <input type="hidden" name="token" value="">
            <input type="hidden" name="qa_id" value="{{ qa.qa_id }}">
            <input type="hidden" name="qa_related" value={{ request.query_params.get('qa_related', '') }}>
            <input type="hidden" name="qa_html" value="1">
            <div class="form_01">
                <div class="bo_w_select position-relative my-3">
                  <label for="qa_category" class="blind">分类<strong>必不可少的</strong></label>
                  <select name="qa_category" id="qa_category" class="frm_input w-100" required="">
                      <option value="">选择分类</option>
                      {% for category in categories %}
                          <option value="{{ category }}" {{ get_selected(qa.qa_category, category) }}>{{ category }}</option>
                      {% endfor %}
                  </select>
                </div>
                <ul class="p-0 row g-3">
                    {% if qa_config.qa_use_email and qa.qa_type != 1 %}
                    {% set required = "required" if qa_config.qa_req_email else "" %}
                    <li class="bo_w_mail chk_box col-6">
                        <label for="qa_email" class="blind">电子邮件 </label>
                        <input type="text" name="qa_email" value="{{ qa.qa_email|default(request.state.login_member.mb_email) }}" id="qa_email" class="{{ required }} frm_input full_input email" size="50" maxlength="100" placeholder="电子邮件" {{ required }}>
                        <input type="checkbox" name="qa_email_recv" id="qa_email_recv" value="1" class="selec_chk" {{ "checked" if qa.qa_email_recv else "" }}>
                        <label for="qa_email_recv" class="frm_info"><span>&nbsp;</span>获取答案</label>
                    </li>
                    {% endif %}
                    {% if qa_config.qa_use_hp and qa.qa_type != 1 %}
                    {% set required = "required" if qa_config.qa_req_hp else "" %}
                    <li class="bo_w_hp chk_box col-6">
                        <label for="qa_hp" class="blind">移动电话</label>
                        <input type="text" name="qa_hp" value="{{ qa.qa_hp|default(request.state.login_member.mb_hp) }}" id="qa_hp" class="{{ required }} frm_input full_input" size="30" placeholder="移动电话" {{ required }}>
                        <input type="checkbox" name="qa_sms_recv" id="qa_sms_recv" value="1" class="selec_chk" {{ "checked" if qa.qa_sms_recv else "" }}>
                        {% if qa_config.qa_sms %}
                            <label for="qa_sms_recv" class="frm_info"><span>&nbsp;</span>注册答案 SMS接收通知</label>
                        {% endif %}
                    </li>
                    {% endif %}
                </ul>
                <div class="bo_w_sbj mb-3">
                  <label for="qa_subject" class="blind">标题<strong class="blind">必不可少的</strong></label>
                  <input type="text" name="qa_subject" value="{{ qa.qa_subject|default("") }}" id="qa_subject" required="" class="frm_input full_input required" size="50" maxlength="255" placeholder="标题">        
                </div>
                <div class="qa_content_wrap">
                  <label for="qa_content" class="blind">内容<strong class="blind">必不可少的</strong></label>
                  {% if qa_config.qa_use_editor %}
                      {{ editor.body("qa_content", content) }}
                  {% else %}
                      <textarea id="qa_content" name="qa_content" style="width: 100%; height: 300px;">
                          {{- content|safe -}}
                      </textarea>    
                  {% endif %}
                </div>
                <div class="bo_w_flie position-relative my-3">
                    <div class="frm_input">
                        <label for="qa_file_1" class="position-absolute top-50 translate-middle-y start-0 fs-3 text-center px-3" style="color: #b2b2b2;">
                            <i class="fa fa-download" aria-hidden="true"></i>
                            <span class="blind"> 文件 #1</span>
                        </label>
                        <input type="file" name="file1" id="qa_file_1" title="文件的附件 1 :  容量 {{ qa_config.qa_upload_size }} 仅上传字节或更少" class="mt-1" style="padding-left: 50px;">
                        {% if qa.qa_file1 %}
                            <input type="checkbox" id="qa_file_del1" name="file_del1" value="1">
                            <label for="qa_file_del1"><span class="elip" title="{{ qa.qa_source1 }}">{{ qa.qa_source1 }}</span> 删除文件</label>
                        {% endif %}
                    </div>
                </div>
                <div class="bo_w_flie position-relative my-3">
                    <div class="frm_input">
                        <label for="qa_file_2" class="position-absolute top-50 translate-middle-y start-0 fs-3 text-center px-3" style="color: #b2b2b2;">
                            <i class="fa fa-download" aria-hidden="true"></i>
                            <span class="blind"> 文件 #2</span>
                        </label>
                        <input type="file" name="file2" id="qa_file_2" title="文件的附件 2 :  容量 {{ qa_config.qa_upload_size }} 仅上传字节或更少" class="mt-1" style="padding-left: 50px;">
                        {% if qa.qa_file2 %}
                            <input type="checkbox" id="qa_file_del2" name="file_del2" value="1">
                            <label for="qa_file_del2"><span class="elip" title="{{ qa.qa_source2 }}">{{ qa.qa_source2 }}</span> 删除文件</label>
                        {% endif %}
                    </div>
                </div>
            </div>
            <div class="btn_confirm position-relative my-3 text-end">
                <a href="{{ url_for('qa_list') }}" class="btn px-4 fs-4 rounded-2 bg-secondary text-white">取消</a>
                <button type="submit" id="btn_submit" accesskey="s" class="btn px-4 fs-4 rounded-2 main-bg text-white">完成</button>
            </div>
        </form>

        <script>
        function html_auto_br(obj)
        {
            if (obj.checked) {
                result = confirm("你想做一个自动换行符吗?\n\n自动换行符是帖子内容之间换行的地方。<br>转换为标记的能力.");
                if (result)
                    obj.value = "2";
                else
                    obj.value = "1";
            }
            else
                obj.value = "";
        }

        function fwrite_submit(f)
        {
            /*
            var hp = f.qa_hp.value.replace(/[0-9\-]/g, "");
            if(hp.length > 0) {
                alert("手机号码是数字, - 请仅输入.");
                return false;
            }
            */
            document.getElementById("btn_submit").disabled = true;

            f.token.value = generate_token();

            return true;
        }
        </script>
    </section>

    {% if qa_config.qa_content_tail %}
        {{ qa_config.qa_content_tail|safe }}
    {% endif %}
{% endblock content %}